package com.controller;

import com.controller.vo.ResultVO;
import com.entity.Notification;
import com.service.NotificationService;
import com.service.TaskMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.service.TaskMonitorService.MODERATOR;
import static com.service.TaskMonitorService.PRODUCT_EXPERIENCE_OFFICER;

/**
 * ClassName: TaskController
 * Description:
 *
 * @author L_Z_R
 * @version 1.0
 * @create 2025/05/26 19:29
 */
@RestController
public class TaskController extends BaseController {
    @Autowired
    private TaskMonitorService taskMonitorService;
    @Autowired
    private NotificationService notificationService;

    /**
     * 根据用户Id获取任务进度
     *
     * @param accountId 用户Id
     * @return 任务进度
     */
    @GetMapping("/progress")
    public ResultVO<Map<String, Map<String, Object>>> getProgress(@RequestParam("accountId") Integer accountId) {
        Map<String, Map<String, Object>> dailyTaskProgress = taskMonitorService.getDailyTaskProgress(accountId);
        return success(dailyTaskProgress); //  获取用户当日所有任务进度
    }

    /**
     * 签到
     * @param accountId 用户Id
     * @return 签到结果
     */
    @PostMapping("/usersingin")
    public ResultVO<String> signIn(@RequestParam("accountId") Integer accountId) {
        taskMonitorService.checkAndRewardTask(accountId, (byte) 1);//签到
        return success();
    }

    /**
     * 检查用户的活跃度是否达标
     * @param accountId
     * @param day
     * @return
     */
    @GetMapping("getUserActivity")
    public ResultVO<Map<String, Object>> getUserActivity(@RequestParam("accountId") Integer accountId, @RequestParam("day") int day) {
        Map<String, Object> userActivity = taskMonitorService.getUserActivity(accountId, day);
        return success(userActivity);
    }

    /**
     * 评估用户活跃度是否达标
     */
    @GetMapping("/evaluate")
    public ResultVO<String> evaluateUserActivity(@RequestParam("accountId") Integer accountId) {
        double v = taskMonitorService.evaluateUserActivity(accountId);
        if (v >= PRODUCT_EXPERIENCE_OFFICER && v < MODERATOR) {
            Notification notification = new Notification(null,  accountId, "系统评估结果", "恭喜您,根据系统检测,您的活跃度已经达标,可以申请成为我们的产品体验官啦!", (byte) 1, false, null);
            notificationService.insert(notification);
        } else if (v >= MODERATOR) {
            Notification notification = new Notification(null,  accountId, "系统评估结果", "恭喜您,根据系统检测,您的活跃度已经达标,可以申请成为我们的版主啦!", (byte) 1, false, null);
            notificationService.insert(notification);
        }
        return success("用户产品体验官与版主评估不达标");
    }

    //根据用户Id添加模拟数据,让该用户的评估达标
    @PostMapping("/mockData")
    public ResultVO<Map<String, Object>> addMockData(@RequestParam("accountId") Integer accountId) {
        Map<String, Object> stringObjectMap = taskMonitorService.addMockActivityDataWithDebugInfo(accountId);
        return success(stringObjectMap);
    }
}
